package com.kindergarten.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kindergarten.annotaion.KinderGartenLogger;
import com.kindergarten.pojo.Food;
import com.kindergarten.pojo.Notice;
import com.kindergarten.service.IFoodService;
import com.kindergarten.utils.DataResults;
import com.kindergarten.utils.MMGridPageVoBean;
import com.kindergarten.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bruce
 * @since 2022-10-26
 */
@Controller
@RequestMapping("/food-api")
public class FoodController {

    @Autowired
    IFoodService foodService;

    /**
     * 01-跳转到模板引擎页面【目标方法】
     * @return
     */
    @GetMapping("topage")
    @KinderGartenLogger(logDescription = "菜谱模块-跳转菜谱页面",methodType = "java.lang.String")
    public String topage() {
        return "food";
    }

    /**
     * 02-数据加载分页 【目标方法】
     * @return
     */
    @ResponseBody
    @RequestMapping("page")
    @KinderGartenLogger(logDescription = "菜谱模块-菜谱数据加载",methodType = "com.kindergarten.utils.MMGridPageVoBean")
    public MMGridPageVoBean page(Food food) {

        MMGridPageVoBean pageVoBean = new MMGridPageVoBean();
        QueryWrapper<Food> queryWrapper = new QueryWrapper<Food>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("del",0); //删除不查询
        queryWrapper.ge(food.getStartTime() != null && !"".equals(food.getStartTime()), "create_time", food.getStartTime());
        queryWrapper.le(food.getEndTime() != null && !"".equals(food.getEndTime()), "create_time", food.getEndTime());
        IPage<Food> page = foodService.page(new Page<Food>(food.getPage(), food.getLimit()), queryWrapper);
        pageVoBean.setRows(page.getRecords()); //每页数据
        pageVoBean.setTotal(page.getTotal()); //总条数
        return pageVoBean;
    }

    /**
     * 02-跳转到模板引擎页面
     * @return
     */
    @GetMapping("toaddpage")
    @KinderGartenLogger(logDescription = "菜谱模块-跳转菜谱新增页面")
    public String toaddpage() {
        return "foodAdd";  //模板引擎页面
    }

    /**
     * 新增
     * @return
     */
    @ResponseBody
    @PostMapping("add")
    @KinderGartenLogger(logDescription = "菜谱模块-新增菜谱数据",methodType="com.kindergarten.utils.DataResults")
    public DataResults add(Food food){
        return DataResults.success(ResultCode.SUCCESS,foodService.save(food));
    }

    /**
     * 02-跳转到模板引擎页面
     * @return
     */
    @GetMapping("toupdatepage")
    @KinderGartenLogger(logDescription = "菜谱模块-跳转菜谱更新页面")
    public String toupdatepage(Integer id, Model model) {
        //数据回显用的
        Food food = foodService.getById(id);
        model.addAttribute("foodObject",food);
        return "foodUpdate";  //模板引擎页面
    }


    /**
     * 更新
     * @return
     */
    @ResponseBody
    @PutMapping("update")
    @KinderGartenLogger(logDescription = "菜谱模块-更新菜谱数据",methodType="com.kindergarten.utils.DataResults")
    public DataResults update(Food food){
        return DataResults.success(ResultCode.SUCCESS,foodService.updateById(food));
    }

    /**
     * 删除
     * @return
     */
    @ResponseBody
    @DeleteMapping("delete/{id}")
    @KinderGartenLogger(logDescription = "菜谱模块-删除菜谱数据",methodType="com.kindergarten.utils.DataResults")
    public DataResults delete(@PathVariable("id") Integer id){
        Food food = new Food();
        food.setId(id);
        food.setDel(1);
        return DataResults.success(ResultCode.SUCCESS,foodService.updateById(food));
    }

}
